/*******************************************************************************
Paper title: Does early nutrition predict cognitive skills during later childhood?
Evidence from two developing countries
Alan Sanchez, Marta Favara, Margaret Sheridan, Jere Behrman.
Created: 8 OCt 2020
This version: 4 Dec 2023 
Structure: Estimations use adjusted HAZ/stunting and OLS methods
Content: Table 2, Table 3 (panel A), Table 6 (panel A), Table 7 (panel A), Table A2, Table B1, Table C1, Table C2 (panel A)	
*******************************************************************************/
			
clear all
local user Alan
		
if "`user'"=="Alan" {
global outdata	C:\Users\alans\Dropbox\_NIHR21_Proposal\NIH Papers\Paper 1_nutrition&EF\paper\WD\Replication files\Data
global output   C:\Users\alans\Dropbox\_NIHR21_Proposal\NIH Papers\Paper 1_nutrition&EF\paper\WD\Replication files\Output
}

global control0   chage_r4 chage_r4_2 female dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c
global country1   mom_spanish
global country2   mom_oromifa mom_tigrina mom_other
global control2q  wi_cq2 wi_cq3 wi_cq4 wi_cq5
global alltask 	  hr2 hr3 wkend /*practices*/
global cluster_r1 sib_y sib_o /*dclustid_r11- dclustid_r120*/

global control0xa  chage_r4xdm_educ2 chage_r4xdm_educ3 chage_r4xdm_educ4 ///
                   chage_r4xurban_c chage_r4xhhsize_c
global control2qa  chage_r4xwi_cq2 chage_r4xwi_cq3 chage_r4xwi_cq4 chage_r4xwi_cq5  
global country1a   chage_r4xmom_spanish 
global country2a   chage_r4xmom_oromifa chage_r4xmom_tigrina chage_r4xmom_other
global alltaska    chage_r4xhr2 chage_r4xhr3 chage_r4xwkend 
global cluster_r1a chage_r4xsib_y chage_r4xsib_o

global control0xf  i.female*chage_r4 i.female*chage_r4_2 i.female*dm_educ2 ///
                   i.female*dm_educ3 i.female*dm_educ4 i.female*urban_c i.female*hhsize_c
global control2qf  i.female*wi_cq2 i.female*wi_cq3 i.female*wi_cq4 i.female*wi_cq5  
global country1f   i.female*mom_spanish 
global country2f   i.female*mom_oromifa i.female*mom_tigrina i.female*mom_other
global alltaskf    i.female*hr2 i.female*hr3 i.female*wkend 
global cluster_r1f i.female*sib_y i.female*sib_o

global task1 	task1
global task2 	itask2_a
global task3 	itask3
global task4 	itask4	

global btask1 	btask1
global btask2 	bitask2_a
global btask3 	bitask3
global btask4 	bitask4	

global sample1  zhfa_c<=6 & zhfa_c>=-6
global sample2  zhfa_c<=6 & zhfa_c>=-6

global absorb   clustid_r1
global indlevel pride_index_r4 agency_index_r4 sesteem_index_r4 ppvtz_r4 missing_ppvt

*---------------------------------------------------------------------------------------------------------------------*---------------------------------------------------------------------------------------------------------------------*------------------------------------------------------------------------------------------------------------------*/
use "$outdata\pe_et_childlevel_230821", clear

bysort childid sibling: egen n=max(task) /* keep only paired-siblings who answer all tasks*/
drop if n!=4 /*22 obs*/
drop n
est clear

gen sib_y=0
gen sib_o=0
replace sib_y=1 if sibling==1 & sibyounger==1
replace sib_o=1 if sibling==1 & sibyounger==0 

gen zhfa_c=.
replace zhfa_c=zhfa_r3 if sibling==1
replace zhfa_c=zhfa_r2 if sibling==0

gen stunting_c=.
replace stunting_c=stunting_r3 if sibling==1
replace stunting_c=stunting_r2 if sibling==0

gen zhfaxsib_y=zhfa_c*sib_y
gen zhfaxsib_o=zhfa_c*sib_o
gen zhfaxage=zhfa_c*chage_r4

gen stuntingxsib_y=stunting_c*sib_y
gen stuntingxsib_o=stunting_c*sib_o
gen stuntingxage  =stunting_c*chage_r4

gen zhfaxfem=zhfa_c*female

gen stuntingxfem=stunting_c*female

gen chage_r4_2=chage_r4*chage_r4

label var stunting_c     "Stunted"
label var stuntingxsib_y "Stunted x younger sib"
label var stuntingxsib_o "Stunted x older sib"
label var stuntingxfem   "Stunted x female"

label var zhfa_c         "Height for age"
label var zhfaxsib_y     "Height for age x younger sib"
label var zhfaxsib_o     "Height for age x older sib"
label var zhfaxfem       "Height for age x female"

label var chage_r4     "Age in months, r4"
label var chage_r4_2   "Age in months squared, r4"
label var female       "Child is female"
label var dm_educ2     "Maternal edu: complete primary"
label var dm_educ3     "Maternal edu: complete secondary"
label var dm_educ4     "Maternal edu: complete tertiary"
label var urban_r1     "Urban area, r1"
label var hhsize_r2    "Household size, r2" 
label var mom_spanish  "Maternal native tongue: spanish"
label var mom_oromifa  "Maternal native tongue: oromifah" 
label var mom_tigrina  "Maternal native tongue: tigrina"
label var mom_other    "Maternal native tongue: other"
label var wi_r1q2      "Wealth index quintile 2"
label var wi_r1q3      "Wealth index quintile 3"
label var wi_r1q4      "Wealth index quintile 4"
label var wi_r1q5      "Wealth index quintile 5"

***Index children
gen     zhfa_5 =zhfa_r2     if (zhfa_r2>=-6 & zhfa_r2<=6) & sibling==0                 & country==2 
gen     zhfa_12=zhfa_r4     if (zhfa_r4>=-6 & zhfa_r4<=6) & sibling==0                 & country==2 

***Older siblings
replace zhfa_12=zhfa_r3 if (zhfa_r3>=-6 | zhfa_r3<=6) & sibling==1 & sibyounger==0 & country==2 

***PPVT
gen missing_ppvt=.
replace missing_ppvt=0 if country==2 & ppvtz_r4!=.
replace missing_ppvt=1 if country==2 & ppvtz_r4==. 
label var missing_ppvt "Missing PPVT"
tab missing_ppvt
replace ppvtz_r4=10000 if ppvtz_r4==.

***Socio emotional outcomes
gen missing_pride_r4=.
replace missing_pride=0 if country==2 & pride_index_r4!=.
replace missing_pride=1 if country==2 & pride_index_r4==. 
label var missing_pride "Missing pride index"
tab missing_pride
replace pride_index_r4=10000 if pride_index_r4==.

gen missing_agency_r4=.
replace missing_agency=0 if country==2 & agency_index_r4!=.
replace missing_agency=1 if country==2 & agency_index_r4==. 
label var missing_agency "Missing agency index"
tab missing_agency
replace agency_index_r4=10000 if agency_index_r4==.

gen missing_sesteem_r4=.
replace missing_sesteem=0 if country==2 & sesteem_index_r4!=.
replace missing_sesteem=1 if country==2 & sesteem_index_r4==. 
label var missing_sesteem "Missing self-esteem index"
tab missing_sesteem
replace sesteem_index_r4=10000 if sesteem_index_r4==.

foreach x of varlist zhfa_5 zhfa_12 pride_index_r4 agency_index_r4 sesteem_index_r4 ppvtz_r4 missing_ppvt ///
hsleep_r4 hcare_r4 hchore_r4 htask_r4 hwork_r4 hschool_r4 hstudy_r4 hplay_r4 female ///
dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c mom_oromifa mom_tigrina mom_other wi_cq2 wi_cq3 wi_cq4 wi_cq5 {
	gen `x'xage  =. 
	gen `x'xage2 =.
	replace `x'xage  =`x'*chage_r2           if sibling==0 
	replace `x'xage  =`x'*chage_r3           if sibling==1
	replace `x'xage2 =`x'*chage_r2*chage_r2  if sibling==0
	replace `x'xage2 =`x'*chage_r3*chage_r3  if sibling==1
}

forvalues x= 1/20 {
	gen clust`x'_=0
	replace clust`x'_=1 if clustid_r1==`x'
}

********************************************************************************
*** Adjusted HAZ for older siblings in Ethiopia
********************************************************************************
gen zhfa_or=zhfa_c
gen stunting_o=stunting_c
sort  country childid sibling

merge country childid sibling using "$outdata\lasso_prediction_older.dta"
drop _merge

replace zhfa_c      =zhfa_5_p               if sibling==1 & sibyounger==0 & country==2
replace zhfaxsib_y  =zhfa_c*sib_y           if sibling==1 & sibyounger==0 & country==2
replace zhfaxsib_o  =zhfa_c*sib_o           if sibling==1 & sibyounger==0 & country==2
replace zhfaxage    =zhfa_c*chage_r4        if sibling==1 & sibyounger==0 & country==2

tab     stunting_c                 		    if sibling==1 & sibyounger==0 & country==2 & task1!=.
replace stunting_c    =1          		    if zhfa_c<-2  & zhfa_c!=. & sibling==1 & sibyounger==0 & country==2
replace stunting_c    =0          		    if zhfa_c>=-2 & zhfa_c!=. & sibling==1 & sibyounger==0 & country==2
replace stuntingxsib_y=stunting_c*sib_y  	if sibling==1 & sibyounger==0 & country==2
replace stuntingxsib_o=stunting_c*sib_o   	if sibling==1 & sibyounger==0 & country==2
replace stuntingxage  =stunting_c*chage_r4 	if sibling==1 & sibyounger==0 & country==2
tab     stunting_c                   		if sibling==1 & sibyounger==0 & country==2 & task1!=.

********************************************************************************
*** Adjusted for index children and younger siblings in Peru & Ethiopia
********************************************************************************
sort  country childid sibling
merge country childid sibling using "$outdata\prediction_index_younger.dta"
drop _merge

replace zhfa_c      =zhfa_5_p1              if sibyounger!=0
replace zhfaxsib_y  =zhfa_c*sib_y           if sibyounger!=0
replace zhfaxsib_o  =zhfa_c*sib_o           if sibyounger!=0
replace zhfaxage    =zhfa_c*chage_r4        if sibyounger!=0

tab     stunting_c country         		    if sibyounger!=0 & task1!=.
replace stunting_c    =1          		    if zhfa_c<-2  & zhfa_c!=. & sibyounger!=0
replace stunting_c    =0          		    if zhfa_c>=-2 & zhfa_c!=. & sibyounger!=0
replace stuntingxsib_y=stunting_c*sib_y  	if sibyounger!=0
replace stuntingxsib_o=stunting_c*sib_o   	if sibyounger!=0
replace stuntingxage  =stunting_c*chage_r4 	if sibyounger!=0
tab     stunting_c                   		if sibyounger!=0 & task1!=.

******************************
*** Results
******************************

***Requires for pooled sample estimations 
replace mom_spanish=0 if country==2 
replace mom_oromifa=0 if country==1  
replace mom_tigrina=0 if country==1 
replace mom_other=0   if country==1 

***Interactions by age
gen chage_r4xbtask1=chage_r4*$btask1
gen chage_r4xbtask2=chage_r4*$btask2
gen chage_r4xbtask3=chage_r4*$btask3
gen chage_r4xbtask4=chage_r4*$btask4

***Interactions by sex
gen chage_r4xfemale     =chage_r4*female 
gen chage_r4xdm_educ2   =chage_r4*dm_educ2 
gen chage_r4xdm_educ3   =chage_r4*dm_educ3 
gen chage_r4xdm_educ4   =chage_r4*dm_educ4
gen chage_r4xurban_c    =chage_r4*urban_c 
gen chage_r4xhhsize_c   =chage_r4*hhsize_c
gen chage_r4xmom_spanish=chage_r4*mom_spanish
gen chage_r4xmom_oromifa=chage_r4*mom_oromifa  
gen chage_r4xmom_tigrina=chage_r4*mom_tigrina 
gen chage_r4xmom_other  =chage_r4*mom_other
gen chage_r4xwi_cq2     =chage_r4*wi_cq2 
gen chage_r4xwi_cq3     =chage_r4*wi_cq3 
gen chage_r4xwi_cq4     =chage_r4*wi_cq4 
gen chage_r4xwi_cq5     =chage_r4*wi_cq5
gen chage_r4xhr2        =chage_r4*hr2 
gen chage_r4xhr3        =chage_r4*hr3
gen chage_r4xwkend      =chage_r4*wkend 
gen chage_r4xpractices  =chage_r4*practices
gen chage_r4xsib_y      =chage_r4*sib_y 
gen chage_r4xsib_o      =chage_r4*sib_o

***Different cluster IDs are required for the pooled sample of two countries
replace clustid_r1=clustid_r1+100 if country==2

global country   mom_spanish mom_oromifa mom_tigrina mom_other

global nut    stunting_c
global nutage stunting_c stuntingxage chage_r4 
global nutsex stunting_c i.female*stunting_c

******************************
*** Table 2, panel A (bivariate model) 
******************************

***Pooled
reg $task3 $nut                                          $btask3   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
estimates store reg5
reg $task2 $nut                                          $btask2   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
estimates store reg2
reg $task1 $nut                                          $btask1   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
estimates store reg1
reg $task4 $nut                                          $btask4   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
estimates store reg4

xi: reg $task3 i.country*stunting_c                          $btask3   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
xi: reg $task2 i.country*stunting_c                          $btask2   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
xi: reg $task1 i.country*stunting_c                          $btask1   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
xi: reg $task4 i.country*stunting_c                          $btask4   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 

***PERU
reg $task3 $nut                                          $btask3   $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) 
estimates store reg5p
reg $task2 $nut                                          $btask2   $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) 
estimates store reg2p
reg $task1 $nut                                          $btask1   $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) 
estimates store reg1p
reg $task4 $nut                                          $btask4   $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) 
estimates store reg4p

***ETHIOPIA
reg $task3 $nut                                          $btask3   $cluster_r1 if country==2 & $sample2 , vce(cluster clustid_r1) 
estimates store reg5e
reg $task2 $nut                                          $btask2   $cluster_r1 if country==2 & $sample2 , vce(cluster clustid_r1) 
estimates store reg2e
reg $task1 $nut                                          $btask1   $cluster_r1 if country==2 & $sample2 , vce(cluster clustid_r1) 
estimates store reg1e
reg $task4 $nut                                          $btask4   $cluster_r1 if country==2 & $sample2 , vce(cluster clustid_r1) 
estimates store reg4e

xml_tab reg5 reg2 reg1 reg4 reg5p reg2p reg1p reg4p reg5e reg2e reg1e reg4e, /// 
save("$output\reg_071023_1.xls") replace tstat below sheet("Table2A") stats(N r2_a)

******************************
*** Table 2, panel B (bivariate model with haz)
******************************
global nut zhfa_c

***Pooled sample
reg $task3 $nut                                         $btask3   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
estimates store reg5
reg $task2 $nut                                         $btask2   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
estimates store reg2
reg $task1 $nut                                         $btask1   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
estimates store reg1
reg $task4 $nut                                         $btask4   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
estimates store reg4

xi: reg $task3 i.country*stunting_c                          $btask3   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
xi: reg $task2 i.country*stunting_c                          $btask2   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
xi: reg $task1 i.country*stunting_c                          $btask1   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 
xi: reg $task4 i.country*stunting_c                          $btask4   $cluster_r1 if $sample1 , vce(cluster clustid_r1) 

***PERU
reg $task3 $nut                                         $btask3   $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) 
estimates store reg5p
reg $task2 $nut                                         $btask2   $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) 
estimates store reg2p
reg $task1 $nut                                         $btask1   $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) 
estimates store reg1p
reg $task4 $nut                                         $btask4   $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) 
estimates store reg4p

***ETHIOPIA
reg $task3 $nut                                         $btask3    $cluster_r1 if country==2 & $sample2 , vce(cluster clustid_r1) 
estimates store reg5e
reg $task2 $nut                                         $btask2    $cluster_r1 if country==2 & $sample2 , vce(cluster clustid_r1) 
estimates store reg2e
reg $task1 $nut                                         $btask1    $cluster_r1 if country==2 & $sample2 , vce(cluster clustid_r1) 
estimates store reg1e
reg $task4 $nut                                         $btask4    $cluster_r1 if country==2 & $sample2 , vce(cluster clustid_r1) 
estimates store reg4e

xml_tab reg5 reg2 reg1 reg4 reg5p reg2p reg1p reg4p reg5e reg2e reg1e reg4e, /// 
save("$output\reg_071023_1.xls") append tstat below sheet("Table2B") stats(N r2_a)

******************************
*** Table 3, panel A (main OLS specification)
******************************
global nut    stunting_c

***Pooled
areg $task3 $nut $control0 $control2q $country $alltask $btask3  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5
areg $task2 $nut $control0 $control2q $country $alltask $btask2  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2
areg $task1 $nut $control0 $control2q $country $alltask $btask1  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb) 
estimates store reg1
areg $task4 $nut $control0 $control2q $country $alltask $btask4  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg4

xi: areg $task3 i.country*stunting_c $control0 $control2q $country $alltask $btask3  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
xi: areg $task2 i.country*stunting_c $control0 $control2q $country $alltask $btask2  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
xi: areg $task1 i.country*stunting_c $control0 $control2q $country $alltask $btask1  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb) 
xi: areg $task4 i.country*stunting_c $control0 $control2q $country $alltask $btask4  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)

***PERU
areg $task3 $nut $control0 $control2q $country1 $alltask $btask3  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5p
areg $task2 $nut $control0 $control2q $country1 $alltask $btask2  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2p
areg $task1 $nut $control0 $control2q $country1 $alltask $btask1  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb) 
estimates store reg1p
areg $task4 $nut $control0 $control2q $country1 $alltask $btask4  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg4p

***ETHIOPIA
areg $task3 $nut $control0 $control2q $country2 $alltask $btask3  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5e
areg $task2 $nut $control0 $control2q $country2 $alltask $btask2  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2e
areg $task1 $nut $control0 $control2q $country2 $alltask $btask1  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg1e
areg $task4 $nut $control0 $control2q $country2 $alltask $btask4  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg4e

xml_tab reg5 reg2 reg1 reg4 reg5p reg2p reg1p reg4p reg5e reg2e reg1e reg4e, ///
save("$output\reg_071023_1.xls") append tstat below sheet("Table3A") stats(N r2_a)

************************************
*** Table 6, panel A (baseline as outcome)
************************************

***Pooled sample
areg $btask3 $nut $control0 $control2q $country  $alltask $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5
areg $btask2 $nut $control0 $control2q $country  $alltask $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2

***PERU
areg $btask3 $nut $control0 $control2q $country1 $alltask $cluster_r1 if country==1  & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5p
areg $btask2 $nut $control0 $control2q $country1 $alltask $cluster_r1 if country==1  & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2p

***ETHIOPIA
areg $btask3 $nut $control0 $control2q $country2 $alltask $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5e
areg $btask2 $nut $control0 $control2q $country2 $alltask $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2e

xml_tab reg5 reg2 reg5p reg2p reg5e reg2e, /// 
save("$output\reg_071023_1.xls") append tstat below sheet("Table6A") stats(N r2_a)

******************************
*** Table 7, panel A (interactions with age)
******************************

***Pooled sample
areg $task3 $nutage female dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c chage_r4xfemale ///
$control0xa $control2q $control2qa $country ///
$country1a $country2a $alltask $alltaska $btask3 chage_r4xbtask3  $cluster_r1 $cluster_r1a if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5
areg $task2 $nutage female dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c chage_r4xfemale ///
$control0xa $control2q $control2qa $country ///
$country1a $country2a $alltask $alltaska $btask2 chage_r4xbtask2  $cluster_r1 $cluster_r1a if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2

***PERU
areg $task3 $nutage female dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c chage_r4xfemale ///
$control0xa $control2q $control2qa $country1 ///
$country1a $alltask $alltaska $btask3 chage_r4xbtask3  $cluster_r1 $cluster_r1a if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5p
areg $task2 $nutage female dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c chage_r4xfemale ///
$control0xa $control2q $control2qa $country1 ///
$country1a $alltask $alltaska $btask2 chage_r4xbtask2  $cluster_r1 $cluster_r1a if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2p

***ETHIOPIA
areg $task3 $nutage female dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c chage_r4xfemale ///
$control0xa $control2q $control2qa $country2 ///
$country2a $alltask $alltaska $btask3 chage_r4xbtask3  $cluster_r1 $cluster_r1a if country==2 & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5e
areg $task2 $nutage female dm_educ2 dm_educ3 dm_educ4 urban_c hhsize_c chage_r4xfemale ///
$control0xa $control2q $control2qa $country2 ///
$country2a $alltask $alltaska $btask2 chage_r4xbtask2  $cluster_r1 $cluster_r1a if country==2 & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2e

xml_tab reg5 reg2 reg5p reg2p reg5e reg2e, /// 
save("$output\reg_071023_1.xls") append tstat below sheet("Table7A") stats(N r2_a)

******************************
*** Table 7, panel B (interactions with sex)
******************************

***Pooled sample
xi: areg $task3 $nutsex $control0xf $control2qf $country1f $country2f $alltaskf i.female*$btask3  $cluster_r1f if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5
xi: areg $task2 $nutsex $control0xf $control2qf $country2f $country2f $alltaskf i.female*$btask2  $cluster_r1f if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2

***PERU
xi: areg $task3 $nutsex $control0xf $control2qf $country1f $alltaskf i.female*$btask3  $cluster_r1f if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5p
xi: areg $task2 $nutsex $control0xf $control2qf $country1f $alltaskf i.female*$btask2  $cluster_r1f if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2p

***ETHIOPIA
xi: areg $task3 $nutsex $control0xf $control2qf $country2f $alltaskf i.female*$btask3  $cluster_r1f if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5e
xi: areg $task2 $nutsex $control0xf $control2qf $country2f $alltaskf i.female*$btask2  $cluster_r1f if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2e

xml_tab reg5 reg2 reg5p reg2p reg5e reg2e, /// 
save("$output\reg_071023_1.xls") append tstat below sheet("Table7B") stats(N r2_a)

*****************************************************************************
********                   APPENDIX                               **********
*****************************************************************************

******************************
****TABLE A2*****
******************************

***WM-MD
ttest $task3 if $sample1, by(country)
***IC
ttest $task2 if $sample1, by(country)
***DM
ttest $task1 if $sample1, by(country)
***IL
ttest $task4 if $sample1, by(country)

***WM-MD
ttest $btask3 if $sample1, by(country)
***IC
ttest $btask2 if $sample1, by(country)
***DM
ttest $btask1 if $sample1, by(country)
***IL
ttest $btask4 if $sample1, by(country)

******************************
****TABLE B1*****
******************************

*Ethiopia, Index Children
sum chage_r2 chage_r3 chage_r4 if country==2 & sibling==0 & task1!=., det
*Ethiopia, Younger Siblings
sum          chage_r3 chage_r4 if country==2 & sibling==1 & task1!=. & sibyounger==1, det
*Ethiopia, Older Siblings
sum          chage_r3 chage_r4 if country==2 & sibling==1 & task1!=. & sibyounger==0, det

*Peru, Index Children
sum chage_r2 chage_r3 chage_r4 if country==1 & sibling==0 & task1!=., det
*Peru, Younger Siblings
sum          chage_r3 chage_r4 if country==1 & sibling==1 & task1!=., det /*all siblings are younger*/

******************************
*** Table C1, panel A (main OLS specification)
******************************
global nut    stunting_c

***PERU
areg $task3 $nut $control0 $control2q $country1 $alltask $btask3  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5p
areg $task2 $nut $control0 $control2q $country1 $alltask $btask2  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2p
areg $task1 $nut $control0 $control2q $country1 $alltask $btask1  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb) 
estimates store reg1p
areg $task4 $nut $control0 $control2q $country1 $alltask $btask4  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg4p

***ETHIOPIA
areg $task3 $nut $control0 $control2q $country2 $alltask $btask3  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5e
areg $task2 $nut $control0 $control2q $country2 $alltask $btask2  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2e
areg $task1 $nut $control0 $control2q $country2 $alltask $btask1  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg1e
areg $task4 $nut $control0 $control2q $country2 $alltask $btask4  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg4e

xml_tab reg5p reg2p reg1p reg4p reg5e reg2e reg1e reg4e, ///
save("$output\reg_071023_1.xls") append tstat below sheet("TableC1") stats(N r2_a)

******************************
*** Table C2, panel A (with HAZ instead of stunting)
******************************
global nut zhfa_c

***Pooled
areg $task3 $nut $control0 $control2q $country $alltask $btask3  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5
areg $task2 $nut $control0 $control2q $country $alltask $btask2  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2
areg $task1 $nut $control0 $control2q $country $alltask $btask1  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb) 
estimates store reg1
areg $task4 $nut $control0 $control2q $country $alltask $btask4  $cluster_r1 if $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg4

***PERU
areg $task3 $nut $control0 $control2q $country1 $alltask $btask3  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5p
areg $task2 $nut $control0 $control2q $country1 $alltask $btask2  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2p
areg $task1 $nut $control0 $control2q $country1 $alltask $btask1  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb) 
estimates store reg1p
areg $task4 $nut $control0 $control2q $country1 $alltask $btask4  $cluster_r1 if country==1 & $sample1 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg4p

***ETHIOPIA
areg $task3 $nut $control0 $control2q $country2 $alltask $btask3  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg5e
areg $task2 $nut $control0 $control2q $country2 $alltask $btask2  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg2e
areg $task1 $nut $control0 $control2q $country2 $alltask $btask1  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg1e
areg $task4 $nut $control0 $control2q $country2 $alltask $btask4  $cluster_r1 if country==2  & $sample2 , vce(cluster clustid_r1) absorb($absorb)
estimates store reg4e

xml_tab reg5 reg2 reg1 reg4 reg5p reg2p reg1p reg4p reg5e reg2e reg1e reg4e, ///
save("$output\reg_071023_1.xls") append tstat below sheet("TableC2A") stats(N r2_a)
